package edu.pku.sei.transformation.editor.controller.commands.node;

import org.eclipse.gef.commands.Command;

import edu.pku.sei.modeler.model.core.MetaPackage;
import edu.pku.sei.transformation.editor.internal.TransformationEditorModelRoot;
import edu.pku.sei.transformation.editor.model.MetaModel;

public class MetaModelCreateCommand extends Command {
	private MetaPackage metaModelPkg;
	private MetaModel metaModel;
	public MetaModelCreateCommand(MetaModel metaModel, edu.pku.sei.modeler.model.core.MetaModel projectModel) {
		this.metaModel = metaModel;
		this.metaModelPkg = getMetaModelPkg(projectModel);
	}
	
	@Override
	public boolean canExecute() {
		if (metaModelPkg == null || metaModel == null)
			return false;
		return super.canExecute();
	}
	private MetaPackage getMetaModelPkg(edu.pku.sei.modeler.model.core.MetaModel projectModel) {
		for (Object obj : projectModel.getPackages()) {
			if (obj instanceof MetaPackage
					&& TransformationEditorModelRoot.METAMODEL_PACKAGE
							.equals(((MetaPackage) obj).getName())) {
				return (MetaPackage) obj;
			}

		}
		return null;
	}
	
	@Override
	public void execute() {
		metaModelPkg.getNamedElements().add(metaModel);
	}
	
	@Override
	public void undo() {
		metaModelPkg.getNamedElements().remove(metaModel);
	}
}
